<div xmlns:ext="http://www.extjs.com" class="body-wrap"><div class="inheritance res-block"><pre class="res-block-inner"><a href="output/Ext.data.Association.html" ext:member="" ext:cls="Ext.data.Association">Association</a>
  <img src="resources/elbow-end.gif">HasManyAssociation</pre></div><h1>Class <a href="source/HasManyAssociation.html#cls-Ext.data.HasManyAssociation">Ext.data.HasManyAssociation</a></h1><table cellspacing="0"><tr><td class="label">Package:</td><td class="hd-info">Ext.data</td></tr><tr><td class="label">Defined In:</td><td class="hd-info"><a href="source/HasManyAssociation.html#cls-Ext.data.HasManyAssociation">HasManyAssociation.js</a></td></tr><tr><td class="label">Class:</td><td class="hd-info"><a href="source/HasManyAssociation.html#cls-Ext.data.HasManyAssociation">HasManyAssociation</a></td></tr><tr><td class="label">Extends:</td><td class="hd-info"><a href="output/Ext.data.Association.html" ext:cls="Ext.data.Association" ext:member="">Association</a></td></tr></table><div class="description"><p>Represents a one-to-many relationship between two models. Usually created indirectly via a model definition:</p>
<pre><code>Ext.regModel(<em>'Product'</em>, {
    fields: [
        {name: <em>'id'</em>,      type: <em>'int'</em>},
        {name: <em>'user_id'</em>, type: <em>'int'</em>},
        {name: <em>'name'</em>,    type: <em>'string'</em>}
    ]
});

Ext.regModel(<em>'User'</em>, {
    fields: [
        {name: <em>'id'</em>,   type: <em>'int'</em>},
        {name: <em>'name'</em>, type: <em>'string'</em>}
    ],
    
    hasMany: {model: <em>'Product'</em>, name: <em>'products'</em>}
});
</pre></code>
<p>Above we created Product and User models, and linked them by saying that a User hasMany Products. This gives
us a new function on every User instance, in this case the function is called 'products' because that is the name
we specified in the association configuration above.</p>
<p>This new function returns a specialized <a href="output/Ext.data.Store.html" ext:cls="Ext.data.Store">Store</a> which is automatically filtered to load
only Products for the given model instance:</p>
<pre><code><i>//first, we load up a User <b>with</b> id of 1</i>
<b>var</b> user = Ext.ModelMgr.create({id: 1, name: <em>'Ed'</em>}, <em>'User'</em>);

<i>//the user.products <b>function</b> was created automatically by the association and returns a <a href="output/Ext.data.Store.html" ext:cls="Ext.data.Store">Store</a></i>
<i>//the created store is automatically scoped to the set of Products <b>for</b> the User <b>with</b> id of 1</i>
<b>var</b> products = user.products();

<i>//we still have all of the usual Store functions, <b>for</b> example it<em>'s easy to add a Product <b>for</b> this User</i>
products.add({
    name: '</em>Another Product<em>'
});

<i>//saves the changes to the store - this automatically sets the <b>new</b> Product'</em>s user_id to 1 before saving</i>
products.sync();</code></pre>
<p>The new Store is only instantiated the first time you call products() to conserve memory and processing time,
though calling products() a second time returns the same store instance.</p>
<p><u>Custom filtering</u></p>
<p>The Store is automatically furnished with a filter - by default this filter tells the store to only return
records where the associated model's foreign key matches the owner model's primary key. For example, if a User
with ID = 100 hasMany Products, the filter loads only Products with user_id == 100.</p>
<p>Sometimes we want to filter by another field - for example in the case of a Twitter search application we may
have models for Search and Tweet:</p>
<pre><code><b>var</b> Search = Ext.regModel(<em>'Search'</em>, {
    fields: [
        <em>'id'</em>, <em>'query'</em>
    ],

    hasMany: {
        model: <em>'Tweet'</em>,
        name : <em>'tweets'</em>,
        filterProperty: <em>'query'</em>
    }
});

Ext.regModel(<em>'Tweet'</em>, {
    fields: [
        <em>'id'</em>, <em>'text'</em>, <em>'from_user'</em>
    ]
});

<i>//returns a Store filtered by the filterProperty</i>
<b>var</b> store = <b>new</b> Search({query: <em>'Sencha Touch'</em>}).tweets();</code></pre>
<p>The tweets association above is filtered by the query property by setting the <a href="output/Ext.data.HasManyAssociation.html#Ext.data.HasManyAssociation-filterProperty" ext:member="filterProperty" ext:cls="Ext.data.HasManyAssociation">filterProperty</a>, and is
equivalent to this:</p>
<pre><code><b>var</b> store = <b>new</b> Ext.data.Store({
    model: <em>'Tweet'</em>,
    filters: [
        {
            property: <em>'query'</em>,
            value   : <em>'Sencha Touch'</em>
        }
    ]
});</code></pre></div><div class="hr"></div><a id="Ext.data.HasManyAssociation-configs"></a><h2>Config Options</h2><table cellspacing="0" class="member-table"><tbody><tr><th colspan="2" class="sig-header">Config Options</th><th class="msource-header">Defined By</th></tr><tr class="config-row  inherited"><td class="micon"><a href="#expand" class="exi">&nbsp;</a></td><td class="sig"><a id="Ext.data.Association-associatedModel"></a><b><a href="source/Association.html#cfg-Ext.data.Association-associatedModel">associatedModel</a></b> : String<div class="mdesc">The string name of the model that is being associated with. Required</div></td><td class="msource"><a href="output/Ext.data.Association.html#associatedModel" ext:member="#associatedModel" ext:cls="Ext.data.Association">Association</a></td></tr><tr class="config-row expandable "><td class="micon"><a href="#expand" class="exi">&nbsp;</a></td><td class="sig"><a id="Ext.data.HasManyAssociation-filterProperty"></a><b><a href="source/HasManyAssociation.html#cfg-Ext.data.HasManyAssociation-filterProperty">filterProperty</a></b> : String<div class="mdesc"><div class="short">Optionally overrides the default filter that is set up on the associated Store. If
this is not set, a filter is autom...</div><div class="long">Optionally overrides the default filter that is set up on the associated Store. If
this is not set, a filter is automatically created which filters the association based on the configured 
<a href="output/Ext.data.HasManyAssociation.html#Ext.data.HasManyAssociation-foreignKey" ext:member="foreignKey" ext:cls="Ext.data.HasManyAssociation">foreignKey</a>. See intro docs for more details. Defaults to undefined</div></div></td><td class="msource">HasManyAssociation</td></tr><tr class="config-row expandable "><td class="micon"><a href="#expand" class="exi">&nbsp;</a></td><td class="sig"><a id="Ext.data.HasManyAssociation-foreignKey"></a><b><a href="source/HasManyAssociation.html#cfg-Ext.data.HasManyAssociation-foreignKey">foreignKey</a></b> : String<div class="mdesc"><div class="short">The name of the foreign key on the associated model that links it to the owner
model. Defaults to the lowercased name...</div><div class="long">The name of the foreign key on the associated model that links it to the owner
model. Defaults to the lowercased name of the owner model plus "_id", e.g. an association with a where a
model called Group hasMany Users would create 'group_id' as the foreign key.</div></div></td><td class="msource">HasManyAssociation</td></tr><tr class="config-row  "><td class="micon"><a href="#expand" class="exi">&nbsp;</a></td><td class="sig"><a id="Ext.data.HasManyAssociation-name"></a><b><a href="source/HasManyAssociation.html#cfg-Ext.data.HasManyAssociation-name">name</a></b> : String<div class="mdesc">The name of the function to create on the owner model. Required</div></td><td class="msource">HasManyAssociation</td></tr><tr class="config-row  inherited"><td class="micon"><a href="#expand" class="exi">&nbsp;</a></td><td class="sig"><a id="Ext.data.Association-ownerModel"></a><b><a href="source/Association.html#cfg-Ext.data.Association-ownerModel">ownerModel</a></b> : String<div class="mdesc">The string name of the model that owns the association. Required</div></td><td class="msource"><a href="output/Ext.data.Association.html#ownerModel" ext:member="#ownerModel" ext:cls="Ext.data.Association">Association</a></td></tr><tr class="config-row  inherited"><td class="micon"><a href="#expand" class="exi">&nbsp;</a></td><td class="sig"><a id="Ext.data.Association-primaryKey"></a><b><a href="source/Association.html#cfg-Ext.data.Association-primaryKey">primaryKey</a></b> : String<div class="mdesc">The name of the primary key on the associated model. Defaults to 'id'</div></td><td class="msource"><a href="output/Ext.data.Association.html#primaryKey" ext:member="#primaryKey" ext:cls="Ext.data.Association">Association</a></td></tr><tr class="config-row  "><td class="micon"><a href="#expand" class="exi">&nbsp;</a></td><td class="sig"><a id="Ext.data.HasManyAssociation-storeConfig"></a><b><a href="source/HasManyAssociation.html#cfg-Ext.data.HasManyAssociation-storeConfig">storeConfig</a></b> : Object<div class="mdesc">Optional configuration object that will be passed to the generated Store. Defaults to 
undefined.</div></td><td class="msource">HasManyAssociation</td></tr></tbody></table><a id="Ext.data.HasManyAssociation-props"></a><h2>Public Properties</h2><table cellspacing="0" class="member-table"><tbody><tr><th colspan="2" class="sig-header">Property</th><th class="msource-header">Defined By</th></tr><tr class="property-row  inherited"><td class="micon"><a href="#expand" class="exi">&nbsp;</a></td><td class="sig"><a id="Ext.data.Association-associatedName"></a><b><a href="source/Association.html#prop-Ext.data.Association-associatedName">associatedName</a></b> : String<div class="mdesc">The name of the model is on the other end of the association (e.g. if a User model hasMany Orders, this is 'Order')</div></td><td class="msource"><a href="output/Ext.data.Association.html#associatedName" ext:member="#associatedName" ext:cls="Ext.data.Association">Association</a></td></tr><tr class="property-row  inherited"><td class="micon"><a href="#expand" class="exi">&nbsp;</a></td><td class="sig"><a id="Ext.data.Association-ownerName"></a><b><a href="source/Association.html#prop-Ext.data.Association-ownerName">ownerName</a></b> : String<div class="mdesc">The name of the model that 'owns' the association</div></td><td class="msource"><a href="output/Ext.data.Association.html#ownerName" ext:member="#ownerName" ext:cls="Ext.data.Association">Association</a></td></tr></tbody></table><a id="Ext.data.HasManyAssociation-methods"></a><h2>Public Methods</h2><div class="no-members">This class has no public methods.</div><a id="Ext.data.HasManyAssociation-events"></a><h2>Public Events</h2><div class="no-members">This class has no public events.</div></div>